/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package name.coolwind.cxblog.web;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import name.coolwind.cxblog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author coolwind
 */
@Controller
@RequestMapping("/user")
public class UserController {
    
    @Autowired
    private UserService userService;
    
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String loginPage(LoginCommand loginCommand) {
        return "user/login";
    }

    //TODO use a hash function to hide the password
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String loginAction(HttpServletResponse res, LoginCommand loginCommand, Model model) {
        boolean loginCheck = userService.loginCheck(loginCommand.getUserEmail(),
                loginCommand.getPassword());
        
        if (loginCheck) {
            int userId = userService.getUserIdByUserEmail(loginCommand.getUserEmail());
            Cookie cookie = new Cookie("user_id", String.valueOf(userId));
            if (loginCommand.isStatus()) {
                cookie.setMaxAge(30 * 24 * 60 * 60);
            } else {
                cookie.setMaxAge(24 * 60 * 60);
            }
            cookie.setPath("/CXBlog");
            res.addCookie(cookie);
            
            return "redirect:/";
        } else {
            String errorStr = "用户名或密码错误！";
            model.addAttribute("error", errorStr);
            
            return "user/login";
        }
        
    }
    
    
    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    public String logoutPage(HttpServletResponse res){
        Cookie cookie = new Cookie("user_id",null);
        cookie.setMaxAge(0);
        cookie.setPath("/CXBlog");
        res.addCookie(cookie);
        
        return "redirect:/";
    }
    
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public String infoPage(){
        return "user/info";
    }
    
    @RequestMapping(value = "/info/update", method = RequestMethod.GET)
    public String updateInfoPage(@CookieValue(value = "user_id",required = false) String userId){
        return "user/update";
    }
    
    @RequestMapping(value = "/info/update", method = RequestMethod.POST)
    public String updateInfoAction(){
        return "redirect:/user/info";
    }
    
    @RequestMapping(value = "/contact", method = RequestMethod.GET)
    public String contactPage(){
        return "user/contact";
    }
    
}
